﻿using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Windows.Forms;

namespace CommonUtils
{
    public static class EditorUtil
    {
        private static IServiceProvider _serviceProvider;

        //Use EditorUtil.SetServiceProvider(this); in Package.cs
        public static void SetServiceProvider(IServiceProvider serviceProvider)
        => _serviceProvider = serviceProvider;

        //封装在Microsoft.VisualStudio.Interop.dll里的提示框
        //标题:Microsoft Visual Studio
        public static void ShowMessage(object value, params object[] args)
        {
            var message = value.ToString();
            if (args.Length > 0)
                message = string.Format(message, args);
            VsShellUtilities.ShowMessageBox(_serviceProvider, message, "IDE提示", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
        }

        [Obsolete("log不出来")]
        public static void LogMessage(string message)
        => VsShellUtilities.LogMessage("", message, __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION);

        public static DTE GetIde()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            return Package.GetGlobalService(typeof(DTE)) as DTE;
        }

        public static Documents GetDocuments()
        => GetIde().Documents;

        public static Document GetActiveDocument()
        => GetIde().ActiveDocument;

        //TextDocument 包含了 选中文本 光标位置
        public static TextDocument GetTextDocument()
        => GetActiveDocument()?.Object() as TextDocument;

        public static string GetText(this Document doc)
        => FileUtil.GetText(doc?.FullName);

        public static string GetFullName(this TextDocument doc)
        => doc?.Parent?.FullName;

        public static string GetText(this TextDocument doc)
        => FileUtil.GetText(doc.GetFullName());

        public static string GetText()
        => GetActiveDocument()?.GetText();

        public static void SaveText(string text)
        => FileUtil.Save(GetActiveDocument().FullName, text);

        public static TextSelection GetSelection()
        => GetTextDocument()?.Selection;

        public static void ReplaceSelection(string newText)
        {
            var selection = GetSelection();
            //selection.ReplacePattern(selection.Text, newText);
            selection.ReplaceText(selection.Text, newText);
        }

        public static string GetSelectedText()
        => GetSelection()?.Text;

        public static void Insert(string value)
        => GetSelection()?.Insert(value);

        public static void InsertLine(string value)
        {
            var document = GetTextDocument();
            var selection = document.Selection;
            selection.EndOfLine();
            selection.Insert(value);
        }

        public static void InsertStart(string value)
        {
            var document = GetTextDocument();
            var selection = document.Selection;
            selection.MoveToPoint(document.StartPoint);
            selection.Insert(value);
        }

        public static void InsertEnd(string value)
        {
            var document = GetTextDocument();
            var selection = document.Selection;
            selection.MoveToPoint(document.EndPoint);
            selection.Insert(value);
        }
    }
}
